 #include <QMessageBox>
#include "mainWindows.h"
#include "about.h"
MainWindows::MainWindows()
{
   initUI();
   initCheeseBoard();
}

MainWindows::~MainWindows()
{

}

void MainWindows::createActions()
{
   //New
   actionNew = new QAction(QIcon(":/images/filenew.png"),tr("&New Game"),this);
   actionNew->setShortcut(QKeySequence::New);
   actionNew->setStatusTip(tr("Start a new game"));
   connect(actionNew, SIGNAL(triggered()), this, SLOT(on_actionNew_triggered()));

   //Load
   actionLoad = new QAction(tr("&Load..."),this);
   actionLoad->setShortcut(tr("Ctrl+L"));
   actionLoad->setStatusTip(tr("Load game to back"));
   connect(actionLoad, SIGNAL(triggered()), this, SLOT(on_actionLoad_triggered()));

   //Save
   actionSave = new QAction(QIcon(":/images/filesave.png"),tr("&Save"),this);
   actionSave->setShortcut(QKeySequence::Save);
   actionSave->setStatusTip(tr("Save game to disk"));
   connect(actionSave, SIGNAL(triggered()), this, SLOT(on_actionSave_triggered()));

   //Exit
   actionQuit = new QAction(tr("&Quit"),this);
   actionQuit->setShortcut(tr("Ctrl+Q"));
   actionQuit->setStatusTip(tr("Exit to this application"));
   connect(actionQuit, SIGNAL(triggered()), this, SLOT(on_actionQuit_triggered()));

   //Undo
   actionUndo = new QAction(QIcon(":/images/undo.png"),tr("&Undo"),this);
   actionUndo->setShortcut(QKeySequence::Undo);
   actionUndo->setStatusTip(tr("fallback one step"));
   connect(actionUndo, SIGNAL(triggered()), this, SLOT(on_actionUndo_triggered()));

   //redo
   actionRedo = new QAction(QIcon(":/images/redo.png"),tr("&Redo"),this);
   actionRedo->setShortcut(QKeySequence::Redo);
   actionRedo->setStatusTip(tr("forward one step"));
   connect(actionRedo, SIGNAL(triggered()), this, SLOT(on_actionRedo_triggered()));

   //About
   actionAbout = new QAction(QIcon(":/images/tool_qt.png"),tr("&About Game..."),this);
   actionAbout->setShortcut(tr("Ctrl+B"));
   actionAbout->setStatusTip(tr("About this application"));
   connect(actionAbout, SIGNAL(triggered()), this, SLOT(on_actionAbout_triggered()));
}

void MainWindows::createToolbars()
{
//    mainToolBar->addAction(actionNew);
//    mainToolBar->addAction(actionSave);
//    mainToolBar->addAction(actionAbout);
}

void MainWindows::initUI()
{
    createActions();
    fileMenu = menuBar()->addMenu(tr("&File"));
    fileMenu->addAction(actionNew);
    fileMenu->addAction(actionLoad);
    fileMenu->addAction(actionSave);
    fileMenu->addSeparator();
    fileMenu->addAction(actionQuit);
    menuBar()->addSeparator();
    actionMenu = menuBar()->addMenu(tr("&Action"));
    actionMenu->addAction(actionUndo);
    actionMenu->addAction(actionRedo);
    menuBar()->addSeparator();
    helpMenu = menuBar()->addMenu(tr("&Help"));
    helpMenu->addAction(actionAbout);
}

void MainWindows::initCheeseBoard()
{
    cheeseboard = new CheeseBoard(this,imageWidth,imageHeight);
    cheeseboard->setSceneRect(0, 0, imageWidth, imageHeight);
    cheeseboard->setItemIndexMethod(QGraphicsScene::NoIndex);
    view = new QGraphicsView(cheeseboard);
    view->setRenderHint(QPainter::Antialiasing);
    view->setBackgroundBrush(QPixmap(":/images/cheese.jpg"));
    view->setCacheMode(QGraphicsView::CacheBackground);
    view->setViewportUpdateMode(QGraphicsView::BoundingRectViewportUpdate);
    view->setDragMode(QGraphicsView::ScrollHandDrag);
    view->resize(imageWidth, imageHeight);
    view->setFixedSize(imageWidth+3, imageHeight+3);
    QHBoxLayout *layout = new QHBoxLayout;
    layout->addWidget(view);
    view->show();
    QWidget *widget = new QWidget;
    widget->setLayout(layout);
    setCentralWidget(widget);
    setWindowTitle(tr("Chinese Cheese"));
    setUnifiedTitleAndToolBarOnMac(true);
}

void MainWindows::showWinMsg(const CheesesType type)
{
    QMessageBox msgBox;
    msgBox.setIcon(QMessageBox::Information);
    msgBox.setWindowTitle(tr("Win Message"));

   if( type == eshuai)
      msgBox.setText(tr("The <b>Black</b> Player won!"));
   else if(type == ejiang)
      msgBox.setText(tr("The <b>Red</b> Player won!"));
   else
      msgBox.setText("Error!");

   msgBox.setInformativeText("Do you want to save this game?");
   msgBox.setStandardButtons(QMessageBox::Save | QMessageBox::Discard | QMessageBox::Cancel);
   msgBox.setDefaultButton(QMessageBox::Save);
   int ret = msgBox.exec();
   switch (ret) {
     case QMessageBox::Save:
       {
         // Save
        QMessageBox::about(this, tr("Saved"),tr("The game was saved successfully."));
         if(cheeseboard != NULL)
          cheeseboard->startNewGame();
       }
         break;
     case QMessageBox::Discard:
         {
            if(cheeseboard != NULL)
              cheeseboard->startNewGame();
         }
         break;
     case QMessageBox::Cancel:
         //do nothing
         break;
     default:
         // should never be reached
         break;
   }//end switch
}

void MainWindows::on_actionNew_triggered()
{
    if(cheeseboard != NULL)
     cheeseboard->startNewGame();
}

void MainWindows::on_actionSave_triggered()
{
     //code here
}

void MainWindows::on_actionLoad_triggered()
{
     //code here
}

void MainWindows::on_actionQuit_triggered()
{
     //code here
}
void MainWindows::on_actionUndo_triggered()
{
  cheeseboard->undoAction();
}

void MainWindows::on_actionRedo_triggered()
{
    //code here
}
void MainWindows::on_actionAbout_triggered()
{
    AboutDialog *dialog = new AboutDialog;
    dialog->show();
}
